package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.os.Message
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.delegate.AddResourceActivityDelegate
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.moudel.bean.AddResourceObj
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 单位报到新增资源
 * Created by Chen.qi on 2018/10/27.
 */
class AddResourceActivity : BaseActivityMVP<AddResourceActivityDelegate>() {

    /**
     * 时间
     */
    private var strTimeStr: String? = null


    /**
     * 时间
     */
    private var endTimeStr: String? = null

    /**
     * 方式
     */
    private var type: String? = null

    /**
     *开始时间
     */
    private var longStartTime: Long? = null


    /**
     *结束时间
     */
    private var longEndTime: Long? = null


    private val strList = listOf("有偿", "无偿")

    override fun initData() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        viewDelegate?.setTopBarTitle("新增资源")
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //开始时间
            closeInput()
            ResourceChooseServer.chooseData(this, handler, 8, 3)
        }, R.id.ll_aar_dataTime)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //结束时间
            closeInput()
            ResourceChooseServer.chooseData(this, handler, 9, 3)
        }, R.id.ll_aar_endTime)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //选择方式
            closeInput()

            ResourceChooseServer.getWheelerWayDialog(this, "选择方式", handler, 11, strList)
        }, R.id.ll_aar_supplyWay)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //提交
            val pinglei = viewDelegate?.getPinglei()
            if (TextUtils.isEmpty(pinglei) || TextUtils.isEmpty(strTimeStr) || TextUtils.isEmpty(endTimeStr)
                    || TextUtils.isEmpty(type)
                    || TextUtils.isEmpty(viewDelegate?.getContentStr())) {
                LKToastUtil.showToastShort("请将数据填写完整")
                return@OnClickListener
            }

            if (longStartTime!! > longEndTime!!) {
                LKToastUtil.showToastShort("开始时间不能大于结束时间")
                return@OnClickListener
            }

            val res = AddResourceObj()
            if (type == "有偿") {
                res.reward = 1
                val money = viewDelegate?.getMoney()
                if (TextUtils.isEmpty(money)) {
                    LKToastUtil.showToastShort("请输入金额")
                    return@OnClickListener
                }
                res.amount = money
            } else {
                res.reward = 0
                res.amount = "0"
            }
            res.resourceType = pinglei
            res.startTime = strTimeStr
            res.endTime = endTimeStr
            res.conetnt = viewDelegate?.getContentStr()

            val intent = Intent()
            intent.putExtra("ADD_RESOURCE", res)
            setResult(101, intent)
            finish()
        }, R.id.rl_aar_submit)
    }

    override fun getDelegateClass(): Class<AddResourceActivityDelegate> {
        return AddResourceActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }


    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            8 -> {
                //开始时间
                val dateString: String = msg.obj as String
                strTimeStr = dateString
                longStartTime = LKTimeUtil.getInstance().getLongTime(strTimeStr, "yyyy-MM-dd")
                viewDelegate?.setDataTime(dateString)
            }
            9 -> {
                //结束时间
                val dateString: String = msg.obj as String
                endTimeStr = dateString
                longEndTime = LKTimeUtil.getInstance().getLongTime(endTimeStr, "yyyy-MM-dd")
                viewDelegate?.setEndTime(dateString)
            }

            11 -> {
                //选择方式
                val wayString: String = msg.obj as String
                type = wayString
                if ("有偿" == type) {
                    viewDelegate?.showInputMoney(true)
                } else {
                    viewDelegate?.showInputMoney(false)
                }


                viewDelegate?.setChooseWay(wayString)
            }
        }
    }


}